from flask import Flask, render_template, request, jsonify
import pandas as pd
import random

app = Flask(__name__)

# 加载题库1和题库2的数据
df1 = pd.read_excel("题库1.xlsx")  # 替换为你的题库1文件路径
df2 = pd.read_excel("题库2.xlsx")  # 替换为你的题库2文件路径

questions1 = df1.to_dict(orient="records")
questions2 = df2.to_dict(orient="records")

# 初始化题库状态
remaining_questions1 = list(questions1)
remaining_questions2 = list(questions2)
current_question1 = None
current_question2 = None

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/get_question", methods=["GET"])
def get_question():
    global current_question1, current_question2
    if remaining_questions1:
        current_question1 = random.choice(remaining_questions1)
        remaining_questions1.remove(current_question1)
    if remaining_questions2:
        current_question2 = random.choice(remaining_questions2)
    return jsonify({"number1": current_question1["序号"] if current_question1 else "", "question1": current_question1["题干"] if current_question1 else "题库1中的题目已经抽完了",
                    "number2": current_question2["序号"] if current_question2 else "", "question2": current_question2["题干"] if current_question2 else "题库2中的题目已经抽完了"})

@app.route("/get_answer", methods=["GET"])
def get_answer():
    return jsonify({"answer1": current_question1["答案"] if current_question1 else "没有问题可供查看",
                    "answer2": current_question2["答案"] if current_question2 else "没有问题可供查看"})

@app.route("/reset", methods=["GET"])
def reset():
    global remaining_questions1, remaining_questions2, current_question1, current_question2
    remaining_questions1 = list(questions1)
    remaining_questions2 = list(questions2)
    current_question1 = None
    current_question2 = None
    return "题库已重置"

if __name__ == "__main__":
    app.run(debug=True)
